import os
import extract_msg

def read_fujian(file_name):
    msg = extract_msg.Message(file_name)
    email_name = os.path.splitext(file_name)[0]  # 邮件名称
    for attachment in msg.attachments:
        attachment_name = attachment.longFilename

        new_attachment_name = f"{email_name}_{attachment_name}"  # 新的附件名称
        attachment.save(customPath=os.path.dirname(file_name))

def getFileName(path):
    # 获取指定目录下的所有指定后缀的文件名
    for root, dirs, files in os.walk(path):
        for file in files:
            if os.path.splitext(file)[1] == '.msg':
                # 保存所有附件
                read_fujian(os.path.join(root, file))

if __name__ == '__main__':
    path = '.\invoice'
    getFileName(path)
